package com.design.twords.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.design.twords.R;
import com.design.twords.adatper.SearchAdapter;
import com.design.twords.database.Searchistory;
import com.design.twords.database.Translation_all;
import com.design.twords.database.Word_all;
import com.design.twords.utils.MyApplication;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Create by Alen
 * Time:  17:55
 */
public class SearchActivity extends AppCompatActivity {

    private static final String TAG = "SearchActivity";

    private EditText inputEditText;
    private TextView cancle, clear_tv;
    private RecyclerView history, searchRecycleView;
    private RelativeLayout noWordTips;
    //历史记录视图
    private LinearLayout historyview, clearhistory;

    private SearchAdapter searchAdapter, adapter;
    private ArrayList<String> containwords = new ArrayList<>();

    //private List<Word_all> searchResult = new ArrayList<>();
    private List<Searchistory> historyWords = new ArrayList<>();
    private List<Searchistory> searchResult = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchword);
        MyApplication.getInstance().addActivity(this);
        initView();
        initData();

        searchRecycleView.setLayoutManager(new LinearLayoutManager(this));
        searchAdapter = new SearchAdapter(searchResult, this, R.layout.item_searchitem);
        searchRecycleView.setAdapter(searchAdapter);

        history.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SearchAdapter(historyWords, this, R.layout.item_searchitem);
        history.setAdapter(adapter);
        Log.i(TAG, "historyWords.size" + historyWords.size());


        inputEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    if (historyWords.size() > 0){
                        noWordTips.setVisibility(View.GONE);
                        historyview.setVisibility(View.VISIBLE);
                    }else{
                        noWordTips.setVisibility(View.VISIBLE);
                        historyview.setVisibility(View.GONE);
                    }
                    searchRecycleView.setVisibility(View.GONE);
                } else {
                    Log.i(TAG, s.toString().trim());
                    setData(s.toString().trim());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        clear_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LitePal.deleteAll(Searchistory.class);
                historyview.setVisibility(View.GONE);
                noWordTips.setVisibility(View.VISIBLE);
            }
        });

        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });



    }
    private void searchStatue(){
        searchRecycleView.setVisibility(View.VISIBLE);
        noWordTips.setVisibility(View.GONE);
        historyview.setVisibility(View.GONE);
    }
    private void modelStatue(){
        historyview.setVisibility(View.VISIBLE);
        searchRecycleView.setVisibility(View.GONE);
        noWordTips.setVisibility(View.GONE);
    }
    private void noSearchStatue(){
        searchRecycleView.setVisibility(View.GONE);
        noWordTips.setVisibility(View.VISIBLE);
        historyview.setVisibility(View.GONE);
    }

    private void initData() {
        historyWords = LitePal.findAll(Searchistory.class);
        if (historyWords.size() > 0){
            modelStatue();
        }else{
            noSearchStatue();
        }
    }

    private void setData(String trim) {
        List<Word_all> word_alls = LitePal.where("word like ?", trim + "%").select("wordId", "word", "wordBook").limit(20).find(Word_all.class);
        if (!word_alls.isEmpty()){
            for (Word_all word : word_alls){
                if(!containwords.contains(word.getWord())){
                    searchResult.add(new Searchistory(word.getWordId(), word.getWord(), word.getWordBook(), getTrans(word.getWordId(), word.getWordBook())));
                    containwords.add(word.getWord());
                }
            }
            Log.i(TAG, "word_alls"+word_alls.size());
            searchStatue();
        }else {
            noSearchStatue();
        }
        searchAdapter.setWordItem(searchResult);
        searchAdapter.notifyDataSetChanged();
    }

    private String getTrans(long wordId, String wordBook) {
        List<Translation_all> transList = LitePal.where("wordId = ? and wordBook = ?", wordId+"", wordBook).find(Translation_all.class);
        if(transList.size() > 0){
            if (transList.get(0).getWordType() != null)
                return transList.get(0).getWordType()+": "+transList.get(0).getCHSMeaning();
            else
                return transList.get(0).getCHSMeaning();
        }
        return null;

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initData();


        adapter.setWordItem(historyWords);
        adapter.notifyDataSetChanged();

        searchAdapter.setWordItem(searchResult);
        searchAdapter.notifyDataSetChanged();

    }

    private void initView() {
        inputEditText = findViewById(R.id.searchview_et);
        cancle = findViewById(R.id.searchview_cancle);
        history = findViewById(R.id.searchview_history);
        searchRecycleView = findViewById(R.id.searchview_searchresult);

        noWordTips = findViewById(R.id.searchview_noword);
        historyview = findViewById(R.id.searchview_historyview);
        clearhistory = findViewById(R.id.searchview_clearhistory);

        clear_tv = findViewById(R.id.searchview_clear_tv);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        supportFinishAfterTransition();
        MyApplication.getInstance().removeActivity(this);

    }
}
